package com.ocom.payhandle.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ocom.common.entity.payconfig.EntityVoucherData;
import com.ocom.common.vo.pyhandle.VoucherListVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

@Mapper
public interface IDaoVoucherData extends BaseMapper<EntityVoucherData> {

    @Select({"<script>",
            "select \n" +
                    "d.*,t.par_value  \n" +
                    "FROM voucher_data d \n" +
                    "left join voucher_type t on d.com_id=t.com_id and d.voucher_type_id=t.voucher_type_id  \n" +
                    "where d.com_id=#{comId}  and d.p_id=#{pid} and d.data_status=4  " +
                    "and  d.used_number &lt; d.number  " +
                    "<![CDATA[" +
                    "and  d.start_date <= #{consDate}  and d.stop_date >= #{consDate}    " +
                    "]]>" +
                    " <when test=\"voucher !=null and  voucher ==2 \" > " +
                    "  AND (t.par_value &gt; #{money} or t.par_value = #{money})  " +
                    " order by d.stop_date asc ,t.par_value asc " +
                    " </when> " +
                    " <when test=\"voucher !=null and  voucher ==3 \" > " +
                    "  AND (t.par_value &lt; #{money} or t.par_value = #{money}  )  " +
                    " order by d.stop_date asc ,t.par_value desc " +
                    " </when> " +
                    "  limit 1  " +
                    "</script>"})
    public EntityVoucherData selectVoucher(@Param("comId") Long comId,
                                           @Param("pid") Long pid,
                                           @Param("voucher") Integer voucher,
                                           @Param("money") Integer money,
                                           @Param("consDate") Date consDate);


    @Select({"<script>",
            "select \n" +
                    " V.voucher_type_id as voucherType , V.voucher_no as voucherNo , \n" +
                    "(V.number - V.used_number) as remainNumber , \n" +
                    "T.max_number as voucherMax , T.par_value as parValue, T.dishes   \n" +
                    "FROM voucher_data as  V \n" +
                    "left join voucher_type as T on V.com_id=T.com_id and V.voucher_type_id=T.voucher_type_id  \n" +
                    "where V.com_id=#{comId}  and V.p_id=#{pid} and V.data_status=4  " +
                    "and  V.used_number &lt; V.number  " +
                    " and  ( T.time_zones = '' or  T.time_zones is null" +
                    "  or  FIND_IN_SET(#{consTimeZone} ,T.time_zones )" +
                    ") " +
                    "<![CDATA[" +
                    "and  V.start_date <= #{consDate}  and V.stop_date >= #{consDate}    " +
                    "]]>" +
                    "and  T.cons_type in (0,#{consType})  " +
                    " order by V.stop_date asc ,V.start_date desc " +
                    "</script>"})
    public List<VoucherListVo> selectVoucherList(@Param("comId") Long comId,
                                                 @Param("pid") Long pid,
                                                 @Param("consDate") Date consDate,
                                                 @Param("voucherTypes") String voucherTypes,
                                                 @Param("consTimeZone") Integer consTimeZone,
                                                 @Param("consType") Integer consType);



    @Select({"<script>",
            "select \n" +
                    " T.voucher_type_name,V.voucher_type_id as voucherType , V.voucher_no as voucherNo , \n" +
                    "(V.number - V.used_number) as remainNumber ,V.start_date,V.stop_date, \n" +
                    "T.max_number as voucherMax , T.par_value as parValue, T.dishes   \n" +
                    "FROM voucher_data as  V \n" +
                    "left join voucher_type as T on V.com_id=T.com_id and V.voucher_type_id=T.voucher_type_id  \n" +
                    "where V.com_id=#{comId}  and V.p_id=#{pid} and V.data_status=4  " +
                    "and  V.used_number &lt; V.number  " +
                    "<![CDATA[" +
                    "and  V.start_date <= #{consDate}  and V.stop_date >= #{consDate}    " +
                    "]]>" +
                    " order by V.stop_date  " +
                    "</script>"})
    public List<VoucherListVo> selectUserVoucherList(@Param("comId") Long comId,
                                                 @Param("pid") Long pid,
                                                 @Param("consDate") Date consDate);

}
